package com.shanghaionstar.dealersorder.model;

import android.content.Context;

import com.onstar.cn.api.DealersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.dealer.MaintenanceDealerEntity;
import com.shanghaionstar.dealersorder.bean.DealerOrderList;
import com.shanghaionstar.enumtype.DealerBrandType;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Yonglang Hong on 2016/8/11.
 */
public class DealersOrderModel extends BaseService {

    private Context context;

    public DealersOrderModel(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<DealerOrderList> loadDealers(final String latitude, final String longtitude) {
        return Observable.defer(new Func0<Observable<DealerOrderList>>() {
            @Override
            public Observable<DealerOrderList> call() {
                try {
                    return Observable.just(doLoadDealers(latitude,longtitude));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public DealerOrderList doLoadDealers(String latitude, String longtitude) throws OnStarException {
        String subscriberID = ApplicationUtil.getInstance().getSubscriberID();
        String accountID = ApplicationUtil.getInstance().getAccountId();
        String vin = null;
        vin = ApplicationUtil.getInstance().getVin();
        DealersService service = new DealersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);

        MaintenanceDealerEntity dealerEntity = new MaintenanceDealerEntity();
        dealerEntity.setLatitude(latitude);
        dealerEntity.setLongitude(longtitude);
        if (ApplicationUtil.getInstance().getCurrentVehicle()!=null) {
            dealerEntity.setBrand(DealerBrandType.getDealerBrandTypeByDesc(ApplicationUtil.getInstance().getCurrentVehicle().getModelDesc()).value());
        } else {
            dealerEntity.setBrand(DealerBrandType.CADILLAC.value());
        }
        service.getMaintenanceDealers(requestEntity, subscriberID, accountID, vin,dealerEntity);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doLoadDealers", requestEntity,String.class,String.class);
        String response = NetConnectUtil.getResponse(getDatas(requestEntity));
        DealerOrderList list = GsonHelper.fromJson( response,DealerOrderList.class);

        return list;
    }
}
